#' 根据ao的ID获得本地parquet文件的id
#'
#' @param id ao的非opengwas的ID
#'
#' @return parquet文件名
#' @export
#'
#' @examples
#'
#'  \dontrun{
#'
#' exp <- U1_use_ID(c("I08.gwas.imputed_v3.both_sexes","I08.gwas.imputed_v3.female"))
#' exp <-  exp %>%
#'     split( list(.) )
#'
#'
#' }
#'
U1_use_ID<-function(id=""){

  suppressMessages( ao<- U1_get_ao(database="all")  )

  ID_all <-c()

  if(!identical(id,"") ){
    for (i in 1:length(id) ) {
      message("处理", id[i],"中")
      if( file.exists( paste0(id[i],".parquet" ) ) ){
        message( "已经存在",paste0(id[i],".parquet文件，没有再次处理" ) )
        ID <-paste0(id[i],".parquet" )
      }else{
        ID <- get_ID(id[i],ao=ao)
        gc()
      }

      ID_all <-c(ID_all,ID)
    }
  }


  return( ID_all )

}

# 将数据都处理成parquet文件
get_ID <- function(id = id,ao=ao){

  index = which(ao$id == id)

  sub_ao<-ao[index,]

  if( sub_ao$class== "opengwas"  ){
    return(id)
  }else{

    options( timeout = 1200 )
    #download.file(sub_ao$url,sub_ao$File)
    tryCatch(curl::curl_download(sub_ao$url,sub_ao$File),
             error = function(e){ e }  )

    if( file.exists(sub_ao$File)  ){

      parquet_id<-format_id( sub_ao )

      unlink(sub_ao$File)

      return(parquet_id)

    }else{

      return(paste0(sub_ao$url,"无法下载或下载不成功，请手动下载提供本地文件"))

    }

  }
}

format_id <- function(sub_ao){

  file<-sub_ao$File
  class<- sub_ao$class

  if( class %in% c( "finngen_R10","finngen_R9","finngen_R8","finngen_R7")  ){
    class<-"finngen"
  }
  class(file)=c( class,class(file) )

  id<-format_ids(file,sub_ao)

}

format_ids<-function(file,sub_ao){
  UseMethod( "format_idss" )
}

format_idss.nealelab2<-function(file,sub_ao){

  # strsplit( file,".bgz")[[1]][1]

  # suppressMessages( df <-   data.table::fread( strsplit( file,".bgz")[[1]][1] ) )
  suppressMessages( df <-   readr::read_tsv(  file ) )

  load(paste0(file.path(tools::R_user_dir("Oneclick",
                                          which = "data"), "path.Rdata")))

  variants <-  arrow::open_dataset( file.path(path, "gwasdata/nearlab extra file/variants.parquet") )

  variants <- variants %>%
    dplyr::inner_join( df, by = c("variant"))%>%
    dplyr::collect()

  variants <- variants %>%
    dplyr::select( c('chr', 'pos', 'ref', 'alt',
                     'rsid','info', 'AF','beta','se','pval') )%>%
    dplyr::mutate(Phenotype=sub_ao$trait,
                  id=sub_ao$id,
                  ncase=sub_ao$ncase,
                  ncontrol=sub_ao$ncontrol,
                  samplesize=sub_ao$sample_size)%>%
    dplyr::rename( eaf=AF)
  # 容易出错 https://academic.oup.com/ije/article/49/5/1734/5900777
  variants <- U1_Clean_data(variants,save=TRUE )

  return( paste0( sub_ao$id,".parquet" )  )
}

format_idss.finngen<-function(file,sub_ao){

  df <-   data.table::fread( file )

  df <- df %>%
    dplyr::select( c('#chrom', 'pos', 'ref', 'alt',
                     'rsids','nearest_genes', 'pval','beta','sebeta','af_alt') )%>%
    dplyr::mutate(Phenotype=sub_ao$trait,
                  id=sub_ao$id,
                  ncase=sub_ao$ncase,
                  ncontrol=sub_ao$ncontrol,
                  samplesize=sub_ao$sample_size)
  # 容易出错 https://academic.oup.com/ije/article/49/5/1734/5900777
  df <- U1_Clean_data( df,save=TRUE )

  return( paste0( sub_ao$id,".parquet" )  )

}
